<?php
class Comment extends MY_Controller
{
	public function __construct(){
		parent::__construct();
	}
	
	public function index($page=0){
		$data['title'] = '评论管理';
		$per_page = 20;
		
		$n = $this->db->get('comment');
		
//		print_r($admins);
		$data['list_current'] = array('article','comment');
		$data['js'] = array('comment');
		
		
		$this->load->library ( 'pagination' );		
		$config ['base_url'] = site_url('/sysadmin/comment/index');
		$config ['total_rows'] = $n->num_rows();
		$config ['per_page'] = $per_page;
		$config ['uri_segment'] = 4;
		$this->pagination->initialize ( $config );
		
		$o = $this->db->query("SELECT *  FROM {$this->db->dbprefix}comment 
		LIMIT $page, $per_page");
		$values = $o->result();
		$data['values'] = $values;
		
		$data['pageinfo'] =  $this->pagination->create_links ();		
		$this->load->view('admin/header', $data);
		$this->load->view('admin/comment', $data);
		$this->load->view('admin/footer');
	}
	function add($id=NULL){
		if($this->input->post('content')){
			$n=date ( "Y-m-d H:i:s" );
			$setarr = array (
			'content' => $this->input->post ( 'content' ), 
			'reply'	=> $this->input->post('reply'),
			'updatetime' => $n,
			'replytime' => $n );
			$this->db->where ( 'id', $this->input->post ( 'id' ) );
			if ($this->db->update ( 'comment', $setarr )) {
				echo 1;
			} else {
				echo - 1;
			}
		}else{
			if($id){//编辑时
				$a = $this->db->query("select *
				from {$this->db->dbprefix}comment 
				where id=$id");
				echo json_encode($a->result());
			}else{
				echo '-1';
			}
			
		}
	}
	function delete($id=NULL){
		if($id){
			$id = explode('-', $id);
			$this->db->where_in('id', $id);
			if($this->db->delete('comment')){
				echo 1;
			}else{
				echo -1;
			}
			
		}
	}
}